package com.hjq.base.app

import android.text.TextUtils
import com.hjq.http.EasyConfig
import com.tencent.mmkv.MMKV

class LoginInfoUtils {
    companion object {
        private const val CONFIG_NAME = "loginconfig"
        const val TOKEN = "Authorization" //
        const val WKID = "wkid" //
        const val WKTOKEN = "wktoken" //
        const val SITEID = "site_id" //

        /**
         * 获取MMKV
         *
         * @return
         */
        private fun getMMKV(): MMKV? {
            return MMKV.mmkvWithID(CONFIG_NAME)
        }

        /**
         * 判断用户是否登录
         *
         * @return
         */
        fun isLogin(): Boolean {
            val token = getMMKV()!!.decodeString(TOKEN, "")
            return !TextUtils.isEmpty(token)
        }

        /**
         * 获取用户的配置信息
         *
         * @param paramName
         * @return
         */
        fun getUserInfo(paramName: String?): String? {
            return getMMKV()!!.decodeString(paramName, "")
        }

        /**
         * 保存用户的配置信息
         *
         * @param paramName
         * @param paramValue
         */
        fun saveUserInfo(
            paramName: String?,
            paramValue: String?
        ) {
            if (!TextUtils.isEmpty(paramValue)) {
                getMMKV()!!.encode(paramName, paramValue)
            }
        }

        /**
         * 重置信息
         *
         * @return
         */
        public fun resetUserInfo() {
            // 载入配置文件
            getMMKV()!!.clear()
            EasyConfig.getInstance()
                .removeHeader("Authorization")
        }

        fun resetUserInfo(paramName: String?) {
            // 载入配置文件
            getMMKV()!!.remove(paramName)
        }

        /**
         * 获取用户的Token
         *
         * @return
         */
        fun getToken(): String? {
            val token = getMMKV()!!.decodeString(TOKEN)
            return if (TextUtils.isEmpty(token)) {
                ""
            } else token
        }


        fun getWKId(): String? {
            val token = getMMKV()!!.decodeString(WKID)
            return if (TextUtils.isEmpty(token)) {
                ""
            } else token
        }

        fun getWKToken(): String? {
            val token = getMMKV()!!.decodeString(WKTOKEN)
            return if (TextUtils.isEmpty(token)) {
                ""
            } else token
        }
    }
}